package com.aaa.member.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.member.entity.AccountStatement;
import com.aaa.member.entity.Client;
import com.aaa.member.mapper.AccountStatementMapper;
import com.aaa.member.mapper.ClientMapper;
import com.aaa.member.service.AccountStatementService;
import io.swagger.models.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (AccountStatement)表服务实现类
 *
 * @author chenhao
 * @since 2020-11-17 22:50:07
 */
@Service("accountStatementService")
public class AccountStatementServiceImpl implements AccountStatementService {
    @Autowired(required = false)
    private AccountStatementMapper accountStatementMapper;
    @Autowired(required = false)
    private ClientMapper clientMapper;

    /**
     * 流水表中的金额 Amount充值到客户表的Account——Blance字段中
     * @param accountStatement
     * @return
     */
    @Transactional //开启事务
    @Override
    public Result addAccBlance(AccountStatement accountStatement) {
        //关联金额,原始的余额+流水表中的Amount金额值 得到账户余额
        if (accountStatement.getAmount()!=0){
            Client client = new Client ();
            accountStatementMapper.insertAmount (accountStatement);//新增带的有主键
            //得到流水表充值金额，充值的金额等于成长值
            double amount = accountStatement.getAmount();
            //通过id从流水表中的一条数据
            Client cli = clientMapper.queryById (accountStatement.getClientId ());
            //得到客户表的原始成长值
            double growth = cli.getGrowth ( );
            //客户表新的成长值
            client.setGrowth (amount+growth);
            //客户表原始的余额
            double accountBalance = cli.getAccountBalance ();
            //将余额添加到客户表的账户余额字段中,修改余额
            client.setAccountBalance (amount+accountBalance);
            //关联客户表Id和流水表Id
            client.setId (accountStatement.getClientId ());
            //修改客户表中的余额
            int i = clientMapper.update (client);

            return new Result (ReturnCode.SUCCESS.getCode ()
                    ,ReturnCode.SUCCESS.getMsg (),i);
        }
        return null;
    }
    /**
     * 通过客户id查询相关明细信息

     * @return
     */
    @Override
    public Result selectByClientId(Integer clientId) {
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(),
                accountStatementMapper.selectById (clientId));
    }

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<AccountStatement> queryAllByLimit(int offset, int limit) {
        return this.accountStatementMapper.queryAllByLimit (offset, limit);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param accountStatement 实例对象
     * @return 对象列表
     */
    @Override
    public List<AccountStatement> queryAll(AccountStatement accountStatement) {
        return this.accountStatementMapper.queryAll (accountStatement);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.accountStatementMapper.deleteById (id) > 0;
    }

    @Override
    public int insert(AccountStatement accountStatement) {
        return accountStatementMapper.insert(accountStatement);
    }

    @Override
    public Result incomeRecordTotal(String yearMonth) {
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(),
                accountStatementMapper.incomeRecordTotal(yearMonth));
    }
}